package com.dhsc.mybatis.plunge;

import java.sql.Connection;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class}) })
public class PrintPlunge implements Interceptor
{
	@Override
	public Object intercept(Invocation invocation) throws Throwable
	{
		StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        BoundSql boundSql = statementHandler.getBoundSql();
        String sql = boundSql.getSql();
        log.info("执行的sql:"+sql);
        List<ParameterMapping> params= boundSql.getParameterMappings();
        log.info("sql参数："+params);
        return invocation.proceed();
	}

	@Override
	public Object plugin(Object obj)
	{
		return Plugin.wrap(obj, this);
	}

	@Override
	public void setProperties(Properties properties)
	{	
	}
}
